using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;

public partial class notifications : System.Web.UI.Page
{
    private ArrayList tableHeadersList = new ArrayList();
    private ArrayList tableColumnsList = new ArrayList();
    protected void Page_Load(object sender, EventArgs e)
    {
        String[] headerlist = { "NotificationId", "Subject", "Source", "Sender", "DeliveryDate"};
        tableHeadersList.AddRange(headerlist);

        String[] columnlist = { "NotificationId", "Subject", "Source", "Sender", "DeliveryDate"};
        tableColumnsList.AddRange(columnlist);

        createTableHeaders();

        getNotifications();
    }

    private void createTableHeaders()
    {
        TableHeaderRow headerRow = new TableHeaderRow();
        TableHeaderCell cell = null;

        foreach (String headername in tableHeadersList)
        {
            cell = new TableHeaderCell();
            cell.Text = headername;
            cell.BorderWidth = Unit.Pixel(1);
            headerRow.Cells.Add(cell);
        }

        Table1.Rows.Add(headerRow);
    }

    private void createRow(XmlNode tasknode)
    {
        TableRow row = new TableRow();
        String notificationId = String.Empty;
        

        TableCell cell = null;
        foreach (String tagname in tableColumnsList)
        {
            String data = tasknode.SelectSingleNode(".//*[local-name()='" + tagname + "']").InnerText;
            if (tagname.CompareTo("NotificationId") == 0)
            {
                notificationId = data;
            }
            
            cell = new TableCell();
            cell.Text = data;
            cell.BorderWidth = Unit.Pixel(1);
            row.Cells.Add(cell);
        }

        row.Attributes.Add("onClick", "javascript:window.open('notificationdetails.aspx?notificationid=" + notificationId +  "')");
        row.Attributes.Add("onmouseOver", "javascript:this.style.cursor='hand'");
        Table1.Rows.Add(row);
    }

    private void getNotifications()
    {
        String getNotificationsResponse = Util.run(RequestXML.getNotifications);

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(getNotificationsResponse);
        XmlNodeList notifications = xmldoc.SelectNodes("//*[local-name()='Notification']");

        foreach (XmlNode notificationnode in notifications)
        {
            createRow(notificationnode);
        }
    }
}
